package com.tjtt.tdm.base;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.Menu;
import com.tjtt.tdm.system.service.FineGrainedAuthorityService;

/**
 * CopyRright (c)2012-0418: <天健通泰科技 > Project: 贵航 Module ID: Comments:
 * 缓存全系统中各个用户都可以用到的数据，减少系统与数据库的交互 JDK version used: <JDK1.6> Namespace:
 * com.tjtt.tdm.sysbase.service Author： xueqi Create Date： 2012-04-23 Modified
 * By： Modified Date: Why & What is modified <修改原因描述> Version: <版本号>
 */
public final class StaticDataCache {
	public static Map<Long, Map<Long, List>> roleMenuButtonMap = new ConcurrentHashMap<Long, Map<Long, List>>(); // 获取menubutton的信息，并且按照roleid-->menuid的方式组织成集合Map																											// <Long,
																													// Map<Long,List>>																										
    public static final int RMBM=0;
    public static final int RMBS=1;
	public static Map menuMap = new ConcurrentHashMap<Long, Map<Long, Map>>();

	public static Map<Long, Map<String, Map>> roleMenuButtons = new ConcurrentHashMap<Long, Map<String, Map>>();// 获取menubutton的信息，并且按照roleid-->requesturl的方式组织成集合Map
																												// <Long,
																												// Map<String,
																												// Map>>

	public static String projectName = "";

	/**
	 * 获取当前角色下的所有菜单的按钮并且按照menuid来分开放在map容器中。
	 * 
	 * @return
	 * @throws ServiceException
	 *             创 建 人:xueqi 日 期:2012-04-19 修 改 人: 日 期:
	 */
	public static synchronized void getMenuButtons(List resultList, Long roleId)
			throws ServiceException {
		Map buttonMap = new HashMap();
		Long lastMenuId, currentMenuId;
		lastMenuId = new Long(-1);
		List buttonList = new ArrayList();
		for (Iterator iter = resultList.iterator(); iter.hasNext();) {
			Map element = (Map) iter.next();

			BigDecimal tempBig = (BigDecimal) element.get("menuid");
			currentMenuId = tempBig.longValue();
			if (currentMenuId.equals(lastMenuId)) {
//				System.out.println("lastMenuId" + lastMenuId + "currentMenuId"
//						+ currentMenuId);
				buttonList.add(element);
			} else {
				if (lastMenuId.intValue() == -1) {
					buttonList = new ArrayList();
					buttonList.add(element);
					buttonMap.put(currentMenuId, buttonList);
//					System.out.println("lastMenuId" + lastMenuId
//							+ "currentMenuId" + currentMenuId);
				} else {
//					System.out.println("lastMenuId" + lastMenuId
//							+ "currentMenuId" + currentMenuId);
					buttonMap.put(lastMenuId, buttonList);
					buttonList = new ArrayList();
					buttonList.add(element);

				}
				lastMenuId = currentMenuId;
			}
		}
		if (!buttonMap.containsKey(lastMenuId)) {
			buttonMap.put(lastMenuId, buttonList);

		}
		roleMenuButtonMap.put(roleId, buttonMap);
		getButtonsbyRoleId(resultList, roleId);
	}

	/**
	 * 获取当前角色下的所有菜单的按钮并且按照action的不同，用action做为key值放在在map容器中。
	 * 
	 * @return 创 建 人:xueqi 日 期:2012-04-19 修 改 人: 日 期:
	 */
	public static void getButtonsbyRoleId(List resultList, Long role_Id) {

		Map buttonMap = new HashMap();
		for (Iterator iter = resultList.iterator(); iter.hasNext();) {
			Map element = (Map) iter.next();
			Object requesturl = element.get("requesturl");
			if (element.get("requesturl") != null) {
				if (!"".equals(requesturl)) {
					String tempcase = (String) element.get("case");
					if (tempcase.equals("false")) {
						buttonMap.put( 
								 element.get("requesturl"), element);
					}
				}
			}
		}

		roleMenuButtons.put(role_Id, buttonMap);

	}

	/**
	 * 获取所有的menu信息并以<url,menu>形式保存在容器里
	 * 
	 * @return
	 */
	public static Map getMenuMap(FineGrainedAuthorityService fgaService) {
		List resultList = fgaService.getAllMenus();
		menuMap.clear();
		for (Iterator iter = resultList.iterator(); iter.hasNext();) {
			Menu element = (Menu) iter.next();
			if (element.getUrl() != null) {
				if (element.getUrl().endsWith(".jsp")) {
					menuMap.put(StaticDataCache.projectName + "/"
							+ element.getUrl(), element);

				}

			}
		}
		return menuMap;
	}
      /**
       * 
       * 刷新该方法中所有的数据
       * @param fgaService
       * @param roleId
       */
	public static void refreshData(FineGrainedAuthorityService fgaService,Long roleId) {
		StaticDataCache.menuMap.clear();
		StaticDataCache.roleMenuButtonMap.clear();
		StaticDataCache.roleMenuButtons.clear();
	 List	 resultList=fgaService.getRoleButtons(roleId);
		getButtonsbyRoleId(resultList, roleId);
		getMenuMap( fgaService);
	}
	 /**
	  * 
	  * 根据roleID 来判断是否存在
	  * @return
	  */
	public static boolean  isExisting(Long [] roleIds,int type)
	{
		for (int i = 0; i < roleIds.length; i++) {
			
		
		
			if(type==StaticDataCache.RMBM)
			{
				
				if(!StaticDataCache.roleMenuButtonMap.containsKey(roleIds[i]))
				{
				return false;
				}
			}else if(type==StaticDataCache.RMBS) {
				
				
				if(!StaticDataCache.roleMenuButtons.containsKey(roleIds[i]))
				{
				return false;
				}
			}
		}
		
		 return true;
		
	}
	
}
